package tool.encode

import androidx.compose.runtime.Composable
import kotlinx.coroutines.runBlocking
import org.jetbrains.compose.resources.getString
import tool.Tool
import tool.ToolEvent
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.Res
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.url_decode
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.url_encode
import java.net.URLDecoder
import java.net.URLEncoder

object URLEncodeTool : Tool {

    private val _name: String by lazy {
        runBlocking {
            getString(Res.string.url_encode)
        }
    }

    override fun getName(): String {
        return _name
    }

    @Composable
    override fun Render(onEvent: (ToolEvent) -> Unit) {
        EncodeAndDecodeToolUI(
            getName(),
            onBackClick = {
                onEvent(ToolEvent.BackEvent)
            }
        ) {
            kotlin.runCatching {
                URLEncoder.encode(it, "UTF-8")
            }.getOrDefault(it)
        }
    }
}

object URLDecodeTool : Tool {

    private val _name: String by lazy {
        runBlocking {
            getString(Res.string.url_decode)
        }
    }

    override fun getName(): String {
        return _name
    }

    @Composable
    override fun Render(onEvent: (ToolEvent) -> Unit) {
        EncodeAndDecodeToolUI(
            getName(),
            onBackClick = {
                onEvent(ToolEvent.BackEvent)
            }
        ) {
            kotlin.runCatching {
                URLDecoder.decode(it, "UTF-8")
            }.getOrDefault(it)
        }
    }

}